﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Saker.Kernel.Model;
using Saker.Kernel.BLL;
using Saker.Utility;
namespace Saker.Web.Boss.Sys.User
{
    public partial class Add : System.Web.UI.Page
    {
        string userID, cmd;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmd = Request["CMD"];
            userID = Request["UserID"];
            if (!IsPostBack)
            {
                if (cmd == "Edit")
                {
                    ShowData();
                }
            }
        }

        protected void ShowData()
        {
            SysUser user = new SysUserBLL().GetByUserID(int.Parse(userID));
            txtUserName.Text = user.UserName;
            txtRealName.Text = user.RealName;
            txtRole.Text = user.RoleID.ToString();
            txtDepart.Text = user.DepartID.ToString();
            txtStatus.Text = user.Status.ToString();
            rblIsLimit.SelectedValue=user.IsLimit==false?"0":"1";
        }
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            SysUser user;
            if(cmd == "Edit")
            {
                user = new SysUserBLL().GetByUserID(int.Parse(userID));
            }
            else
            {
                user = new SysUser();
            }
            user.UserName = txtUserName.Text.Trim();
            user.Password =StringHelper.GetMD5(txtPassword.Text.Trim());
            user.RealName = txtRealName.Text.Trim();
            user.RoleID = int.Parse(txtRole.Text);
            user.DepartID = int.Parse(hdDepartID.Value);
            user.Status = int.Parse(txtStatus.Text);
            user.IsLimit = rblIsLimit.SelectedValue=="0"?false:true;
            user.CreateTime = DateTime.Now;
            user.IsOnline = false;
            SysUserBLL userbll = new SysUserBLL();
            object obj ;
            if (cmd == "Edit")
            {
                obj = userbll.Update(user);
            }
            else
            {
                obj = userbll.Add(user);
            }
            if (null == obj)
            {
                ScriptManager.RegisterStartupScript((Page)this, this.Page.GetType(), "alert", "$(function () { top.jBox.tip('保存失败！', 'error'); });", true);

            }
            else
            {
                ScriptManager.RegisterStartupScript((Page)this, this.Page.GetType(), "alert", "$(function () { top.jBox.tip('保存成功！', 'success'); });", true);
            }
        }

        protected void lbtnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx");
        }
    }
}